package com.xk857.controller;


import com.xk857.pojo.req.WeatherREQ;

import com.xk857.db.CarDB;
import com.xk857.pojo.*;
import com.xk857.pojo.req.UserREQ;
import com.xk857.pojo.vo.*;
import com.xk857.service.CarService;
import com.xk857.util.CarResult;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestBody;


import java.util.HashMap;
import java.util.List;


/**
 * 小车接口模拟 - 单数
 * @Author CV大魔王
 * @Date 2021.1.20
 */
@Api(value = "小车接口模拟",tags = "小车接口 - CV大魔王")
@RestController
@RequestMapping("transportservice/type/jason/action")
public class CVKingController {

    @Autowired
    private CarService carService;

    @ApiOperation("查询天气信息")
    @PostMapping("weather")
    public Weather getWeather(@Validated @RequestBody WeatherREQ req) {
        return carService.getWeather(req);
    }

    /**
     * 0. 用户登陆
     */
    @ApiOperation("设置小车动作")
    @PostMapping("login")
    public HashMap<String, Object> login(@Validated @RequestBody UserREQ user) {
        boolean flag = carService.login(user);
        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("flag",flag);
        if (flag) {
            resultMap.put("msg","登陆成功");
            return resultMap;
        }else {
            resultMap.put("msg","登陆失败");
            return resultMap;
        }
    }

    /**
     * 1. 设置小车动作
     */
    @ApiOperation("设置小车动作")
    @PostMapping("SetCarMove")
    public CarResult setCarAction(@Validated @RequestBody CarStatus car) {
        Car car1 = carService.findCarById(car.getCarId());
        if (car1 == null) {
            return new CarResult("failed");
        }
        return new CarResult("ok");
    }


    /**
     * 2. 查询小车账户余额
     */
    @ApiOperation("查询小车账户余额")
    @GetMapping("GetCarAccountBalance/{carId}")
    public CarBalance getBalance(@ApiParam("小车id") @PathVariable Integer carId){
        Car car = carService.findCarById(carId);
        return new CarBalance(car.getMoney());
    }

    /**
     * 3. 小车账户充值
     */
    @ApiOperation("小车账户充值")
    @PostMapping("SetCarAccountRecharge")
    public CarResult accountRecharge(@Validated @RequestBody CarRecharge carRecharge) {
        Car car = carService.findCarById(carRecharge.getCarId());
        if (car == null) {
            return new CarResult("failed");
        }
        if (carRecharge.getMoney()<=0 ){
            return new CarResult("充值金额必须大于0");
        }
        carService.addMoney(car,carRecharge.getMoney());
        return new CarResult("ok");
    }

    /**
     * 4.小车速度查询
     */
    @ApiOperation("小车速度查询")
    @GetMapping("GetCarSpeed/{carId}")
    public CarSpeed getSpeed(@PathVariable @ApiParam("小车id") Integer carId){
        Car car = carService.findCarById(carId);
        return new CarSpeed(car.getSpeed());
    }

    /**
     * 5.设置红绿灯当前状态
     * 仅模拟，不做实际操作
     */
    @ApiOperation("设置红绿灯当前状态")
    @PostMapping("SetTrafficLightNowStatus")
    public CarResult setTheCurrentStatusOfTrafficLights(@RequestBody @Validated Intersection intersection){
        return new CarResult("ok");
    }

    /**
     * 6.查询红绿灯的配置信息
     */
    @ApiOperation("查询红绿灯的配置信息")
    @GetMapping("GetTrafficLightConfigAction/{trafficLightId}")
    public TrafficLightConfigurationResult queryTheConfigurationOfTrafficLights(@ApiParam("红绿灯id") @PathVariable Integer trafficLightId){
        TrafficLightConfiguration traffic = carService.findTrafficLightConfigurationById(trafficLightId);
        return new TrafficLightConfigurationResult(traffic.getRedTime(),traffic.getGreenTime(),traffic.getYellowTime());
    }

    /**
     * 8.查询所有红绿灯的配置信息
     */
    @ApiOperation("查询所有红绿灯的配置信息")
    @GetMapping("GetTrafficLightConfigActionAll")
    public List<TrafficLightConfiguration> GetTrafficLightConfigActionAll(){
        return carService.findTrafficLightConfigurationAll();
    }

    /**天气
     * 7.查询所有传感器的当前值
     */
    @ApiOperation("查询所有传感器的当前值")
    @GetMapping("GetAllSense")
    public Sense getAllSense(){
        return CarDB.senseList.get((int) (Math.random() * CarDB.senseList.size()));
    }
}
